package cn.itcast.zt.pub;

import cn.hutool.core.util.RandomUtil;
import cn.itcast.zt.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author zhangtian105
 * @ClassName MessageSender.java
 * @projectName Redis
 * @Description 消息发布着
 * @createTime 2020年04月09日 13:40:00
 */
@EnableScheduling
@Component
public class MessageSender {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 功能描述: <br>
     * 〈间隔2秒，通过stringRedisTemplate〉对象向redis消息队列chat频道发送消息
     * @Param: []
     * @Return: void
     * @Author: zhangtian105
     * @Date: 2020/4/9 13:41
     */
    @Scheduled(fixedDelay = 10)
    public void sendChatMessage() {
        User user = User.builder()
                .name("水梦星光"+RandomUtil.randomLong())
                .age(RandomUtil.randomInt(0, 100))
                .address("北京东大街"+RandomUtil.randomString(18))
                .build();
        redisTemplate.convertAndSend("chat", user);
    }

    /**
     * 功能描述: <br>
     * 〈间隔2秒，通过stringRedisTemplate〉对象向redis消息队列play频道发送消息
     * @Param: []
     * @Return: void
     * @Author: zhangtian105
     * @Date: 2020/4/9 13:41
     */
    @Scheduled(fixedDelay = 20)
    public void sendPlayMessage() {
        User user = User.builder()
                .name("明月星辰"+RandomUtil.randomLong())
                .age(RandomUtil.randomInt(0, 100))
                .address("上海西大街"+RandomUtil.randomString(18))
                .build();
        redisTemplate.convertAndSend("play", user);
    }
}
